<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="身份证号" prop="idcard">
        <el-input
          v-model="queryParams.idcard"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入学生姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学校ID" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入学校ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:schoolStudent:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:schoolStudent:edit']"
        >修改</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:schoolStudent:remove']"
        >删除</el-button>
      </el-col> -->

      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['system:schoolStudent:add', 'system:schoolStudent:edit']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:schoolStudent:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="schoolStudentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="学号" align="center" prop="studentNo" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="sex" >
         <template slot-scope="scope">
           <span v-if="scope.row.sex=='0'">男</span>
           <span v-else-if="scope.row.sex=='1'">女</span>
           <span v-else>未知</span>
         </template>
      </el-table-column>
      <el-table-column label="身份证号" align="center" prop="idcard" />
      <el-table-column label="出生日期" align="center" prop="birthday" />
      <el-table-column label="学校" align="center" prop="deptId" />
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">

          <el-switch
            v-model="scope.row.status"
            active-color="#13ce66"
            active-value="0" inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>

        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:schoolStudent:edit']"
          >修改</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:schoolStudent:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学生库对话框 -->
    <Edit v-if="editParams.show"
      :id="editParams.id"
      :idcard="editParams.idcard"
      :title="editParams.title"
      @handleSubmit="handleSubmit"
      @handleClose="handleClose"/>

    <!-- 导入用户数据 -->
    <Import v-if="importParams.show"
      :deptId="importParams.deptId"
      :deptName="importParams.deptName"
      @handleSubmit="handleSubmit"
      @handleClose="handleClose"
    />
  </div>
</template>

<script>
import { listSchoolStudent, getSchoolStudent, delSchoolStudent, addSchoolStudent, updateSchoolStudent } from "@/api/school/schoolStudent";
import Edit from "@/views/school/schoolStudent/components/edit.vue";
import Import from "@/views/school/schoolStudent/components/import.vue";

export default {
  name: "SchoolStudent",
  components: {
    Edit, Import,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学生库表格数据
      schoolStudentList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        idcard: null,
        name: null,
        sex: null,
        deptId: null,
        status: null,
      },
      editParams: {
        show: false,
        id: null,
        idcard: null,
        title: null,
      },
      importParams: {
        show: false,
        deptId: 1992,
        deptName: '世纪末哦',
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询学生库列表 */
    getList() {
      this.loading = true;
      listSchoolStudent(this.queryParams).then(response => {
        this.schoolStudentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    // 提交信息
    handleSubmit(){
      this.getList();
      this.handleClose();
    },
    // 取消按钮
    handleClose() {

      this.editParams.show = false;
      this.editParams.id = null;
      this.editParams.idcard = null;
      this.editParams.title = null;

      this.importParams.show = false;
      this.importParams.deptId = null;
      this.importParams.deptName = null;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.editParams.show = true;
      this.editParams.id = null;
      this.editParams.idcard = null;
      this.editParams.title = "添加学生";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.editParams.show = true;
      this.editParams.id = row.id || this.ids
      this.editParams.idcard = row.idcard || null;
      this.editParams.title = "修改学生";
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.msg("暂时不允许删除学生！");
      // const ids = row.id || this.ids;
      // this.$modal.confirm('是否确认删除学生库编号为"' + ids + '"的数据项？').then(function() {
      //   return delSchoolStudent(ids);
      // }).then(() => {
      //   this.getList();
      //   this.$modal.msgSuccess("删除成功");
      // }).catch(() => {});
    },
    /** 导入 */
    handleImport(){
      this.importParams.show = true;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/schoolStudent/export', {
        ...this.queryParams
      }, `schoolStudent_${new Date().getTime()}.xlsx`)
    },
    /** 修改状态 **/
    handleStatusChange(row){
      console.log('修改状态:', row)
    },
  }
};
</script>
